//
//  BTEProductlistTableViewCell.swift
//  BTE
//
//  Created by 张文 on 2025/8/31.
//

import UIKit

class BTEProductlistTableViewCell: UITableViewCell {
    
    var clickItem:((Int)->())?
    
    let container = UIView()
    
    private var itemViews: [UIView] = []

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        
        contentView.addSubview(container)
        container.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(10)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension BTEProductlistTableViewCell {
    
    @objc func clickSomeItme(w: UITapGestureRecognizer) {
        clickItem?(w.view?.tag ?? 0)
    }
    
    // 配置 cell
      func configure(with count: [BTEProductpeeebonbloidNetModel]) {
          // 清空旧的 view，避免复用叠加
          itemViews.forEach { $0.removeFromSuperview() }
          itemViews.removeAll()
          
          // 创建新的子 view
          for i in 0..<count.count {
              let v = UIView()
              v.roundCorners(.allCorners, radius: 19)
              
              let vtap = UITapGestureRecognizer.init(target: self, action: #selector(clickSomeItme(w:)))
              v.tag = i
              v.addGestureRecognizer(vtap)
              
              
              let image = UIImageView()
              v.addSubview(image)
              image.contentMode = .scaleToFill
              image.snp.makeConstraints { make in
                  make.edges.equalToSuperview()
              }
              
              switch i {
              case 0:
                  image.image = UIImage.init(named: "fewfwfwef32byjhj")
              case 1:
                  image.image = UIImage.init(named: "fwefwefwe23232323")
              case 2:
                  image.image = UIImage.init(named: "fwefwefwef3323232")
              case 3:
                  image.image = UIImage.init(named: "fewfwef2323232321")
              case 4:
                  image.image = UIImage.init(named: "Group 1fewf33333171277323")
              default:
                  break
              }
              
              
              let topimae = UIImageView.init()
              topimae.sd_setImage(with: URL.init(string: count[i].acceptable ?? ""))
              
              let topla = UILabel()
              topla.textColor = .black
              topla.font = .systemFont(ofSize: 14, weight: .bold)
              topla.textAlignment = .center
              
              let im = UIImageView.init(image: UIImage.init(named: "fwefewvref22333333323233"))
              if let age = count[i].chandelled  {
                  im.image = UIImage.init(named: age == "1" ? "fewfewfewfew3333333f3f" : "fwefewvref22333333323233")
              }
              
              v.addSubview(topimae)
              topimae.snp.makeConstraints { make in
                  make.centerX.equalToSuperview()
                  make.width.equalTo(92)
                  make.height.equalTo(68)
                  make.top.equalToSuperview().offset(18)
              }
              
              v.addSubview(topla)
              topla.text = count[i].aftermath
              topla.snp.makeConstraints { make in
                  make.centerX.equalToSuperview()
                  make.top.equalTo(topimae.snp.bottom).offset(5)
              }
              
              v.addSubview(im)
              im.snp.makeConstraints { make in
                  make.centerX.equalToSuperview()
                  make.width.height.equalTo(23)
                  make.top.equalTo(topla.snp.bottom).offset(8)
              }
              
              container.addSubview(v)
              itemViews.append(v)
          }
          
          layoutItems()
      }
      
      private func layoutItems() {
          let columnCount = 2
          let spacing: CGFloat = 15
          
          for (index, subView) in itemViews.enumerated() {
              let row = index / columnCount
              let col = index % columnCount
              
              subView.snp.makeConstraints { make in
                  
                  make.height.equalTo(160)
                  
                  if col == 0 {
                      make.left.equalToSuperview()
                  } else {
                      make.left.equalTo(itemViews[index - 1].snp.right).offset(spacing)
                      make.width.equalTo(itemViews[index - 1])
                  }
                  
                  if col == columnCount - 1 {
                      make.right.equalToSuperview()
                  }
                  
                  if row == 0 {
                      make.top.equalToSuperview()
                  } else {
                      make.top.equalTo(itemViews[index - columnCount].snp.bottom).offset(spacing)
                      
                  }
                  
                  if index >= itemViews.count - 1 {
                      make.bottom.equalToSuperview()
                  }
                  
              }
          }
      }
}
